package javbot;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;

import javax.swing.JOptionPane;

import javbot.gui.JavbotFrame;

public class Javbot {
	
	public static JavbotFrame javbotFrame;
	
	public static void main(String[] args) throws FileNotFoundException {
		
		if (args.length > 0) {
			if (args.length <= 3 && 
					!(GlobalClass.mailProps.getMailServerIn().length() > 0 &&
					 GlobalClass.mailProps.getLogin().length() > 0 && 
					 GlobalClass.mailProps.getPass().length > 0)) {
				System.out.println("Usage: Javbot <mail_server> <login> <password> OR Javbot");
				System.exit(0);
			}
			
			GlobalClass.startMailChecker(args[0], args[1], args[2].toCharArray());
		}
		else {
			try {
				GlobalClass.initKeyData(false);
				javbotFrame = new JavbotFrame();
				javbotFrame.setVisible(true);
				if (GlobalClass.mailProps.isEnableMailChecker())
					GlobalClass.startMailChecker(GlobalClass.mailProps.getMailServerIn(),
							GlobalClass.mailProps.getLogin(),
							GlobalClass.mailProps.getPass());
				else
					GlobalClass.stopMailChecker();				
			} catch (Exception e) {
				handleException(e);
				System.exit(1);
			}
		}
	}

	private static void handleException(Exception e) throws FileNotFoundException {
		JOptionPane.showMessageDialog(null,
			    e.getMessage(),
			    "Error",
			    JOptionPane.WARNING_MESSAGE);
		PrintWriter exceptionFile = new PrintWriter(new File("StackTrace.txt"));
		e.printStackTrace(exceptionFile);
		exceptionFile.close();
	}
}
